package com.itheima.filter;

import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.login.LoginService;
import com.itheima.service.login.impl.LoginServiceImpl;
import com.itheima.service.user.UserService;
import com.itheima.service.user.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebFilter("/pages/login.jsp")
public class CookieFilter implements Filter {
    //创建登录业务层对象
    LoginService loginService = new LoginServiceImpl();
    UserService userService = new UserServiceImpl();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //将请求和响应转换为Http请求和响应
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        //System.out.println("Test");
        //定义账号和密码
        String username = null;
        String password = null;
        //获取Cookie信息
        Cookie[] cookies = req.getCookies();
        if (cookies != null && cookies.length > 0) {
            //判断用户信息
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("username")) {
                    //获取账号
                    username = cookies[i].getValue();
                } else if (cookies[i].getName().equals("password")) {
                    //获取账号和KEY加密后的字符串
                    password = cookies[i].getValue();
                }
            }
        }
        if(StringUtils.isNotBlank(username)){
            UserInfo user = loginService.login(username, password);
            if(user != null){
                //获取到角色权限列表
                List<String> permissions = userService.findPerById(user.getId());
                //封装进user
                List<Permission> userPermissions = new ArrayList<Permission>();
                for (int i = 0; i < permissions.size(); i++) {
                    Permission p = new Permission();
                    p.setUrl(permissions.get(i));
                    userPermissions.add(p);
                }
                Role role = new Role();
                role.setPermissions(userPermissions);
                user.setRole(role);
                //登录成功 把用户对象设置到作用域 并跳转
                req.setAttribute("user",user);
                //登录成功 把用户存到会话域 验证登录使用
                req.getSession().setAttribute("user",user);
                req.getSession().setMaxInactiveInterval(3600);
                //跳转main
                req.getRequestDispatcher("/pages/common/main.jsp").forward(req,resp);
            }else {
                //登录失败 跳转到登录 并弹窗登陆失败
                req.getSession().setAttribute("isNotLogin","1");
                req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
            }
        }

        //放行
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
